﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace SolutionNavigateAddIn
{
    [Serializable]
    public class SnSettings
    {
        private const string filename = "settings.xml";
        
        private bool showSettings = true;
        public bool ShowSettings
        {
            get { return showSettings; }
            set { showSettings = value; }
        }

        private bool canTrace = true;
        public bool CanTrace
        {
            get { return canTrace; }
            set { canTrace = value; }
        }

        private string globalKey;
        public string GlobalKey
        {
            get { return globalKey; }
            set { globalKey = value; }
        }

        public string Version { get; set; }

        public Version GetVersion()
        {
            try
            {
                return new Version(Version);
            }
            catch
            {
                return new Version();
            }
        }

        public static SnSettings Default
        {
            get
            {
                return new SnSettings();
            }
        }

        public static SnSettings GetSettings()
        {
            FileInfo info = GetSettingFilePath();

            if (info.Exists)
            {
                Stream fileStream = null;
                try
                {
                    fileStream = info.Open(FileMode.Open, FileAccess.Read);
                    XmlSerializer serializer = new XmlSerializer(typeof(SnSettings));
                    return (SnSettings)serializer.Deserialize(fileStream);
                }
                catch (Exception)
                {
                    return Default;
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                        fileStream.Dispose();
                    }
                }

            }

            return Default;
        }
        
        public static void SaveSettings(SnSettings settings)
        {
            FileInfo info = GetSettingFilePath();

            Stream fileStream = null;
            try
            {
                fileStream = info.Open(FileMode.Create, FileAccess.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(typeof(SnSettings));
                serializer.Serialize(fileStream,settings);
            }
            finally 
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                }
            }
        }

        private static FileInfo GetSettingFilePath()
        {
            return new FileInfo(Path.Combine(SnEnvironment.ProgramDataDirectory, filename));
        }

    }
}